package com.yuncheng.common.base.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.EasypoiSingleExcelView;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuncheng.common.base.service.CcBaseService;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.common.utils.CcExcelUtil;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Controller类的公共父类
 * @param <T> beanClass
 * @param <S> ServiceClass
 *  xiongda update 20201124
 */
public class CcBaseController<T, S extends CcBaseService<T>> {

    private static final Logger log = LoggerFactory.getLogger(CcBaseController.class);

    @Autowired
    S service;


    protected ModelAndView exportExcel(HttpServletRequest request, T object, Class<T> clazz, String title) {
        QueryWrapper<T> queryWrapper = QueryBuilder.initQueryWrapper(object, request.getParameterMap());
        List<T> pageList = service.list(queryWrapper);
        String selections = request.getParameter("selections");
        List exportList = pageList;
        if(!StringUtils.isEmpty(selections)){
            List<String> selectedList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectedList.contains(getId(item))).collect(Collectors.toList());
        }
        ModelAndView mv = new ModelAndView(new EasypoiSingleExcelView());
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        //多语言处理
        clazz = CcExcelUtil.convertNameForI18n(clazz);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        ExportParams params = new ExportParams(title+I18nUtil.message("export.title"), title, ExcelType.HSSF);
        mv.addObject(NormalExcelConstants.PARAMS, params);
        return mv;

    }


    protected HttpResult<?> importExcel(HttpServletRequest request,  Class<T> beanClazz) {

        try {
            List<T> list = CcExcelUtil.excelRecord2JavaList(request, beanClazz, null);
            try{
                Method validMethod = service.getClass().getMethod("importValid",List.class);
                if(null!=validMethod){
                    String errorMsg = (String)validMethod.invoke(service,list);
                    if(errorMsg != null && errorMsg.trim().length()>0){
                        log.error(errorMsg);
                        return HttpResult.error(I18nUtil.message("excel.import.error")+","+errorMsg);
                    }
                }
            }catch (NoSuchMethodException e){

            }
            return CcExcelUtil.javaList2Db(list, service.getClass());
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return HttpResult.error(I18nUtil.message("excel.import.error"));
        }
    }



    private String getId(T item) {
        try {
            return PropertyUtils.getProperty(item, "id").toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
